package com.excesys.exsecs.energy.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.EnergyRateFlow;
import com.excesys.exsecs.energy.entity.EnergyRateManage;
import com.excesys.exsecs.energy.service.EnergyRateFlowService;
import com.excesys.exsecs.energy.service.EnergyRateManageService;
import com.excesys.exsecs.energy.vo.EnergyRateManageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能源费率管理
 *
 * @author zhang
 * @date 2024-09-27 10:29:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/energyRateManage")
@Tag(description = "energyRateManage", name = "能源费率管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EnergyRateManageController {

    private final EnergyRateManageService energyRateManageService;

    private final EnergyRateFlowService energyRateFlowService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param energyRateManage 能源费率管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_view')")
    public R getEnergyRateManagePage(@ParameterObject Page page, @ParameterObject EnergyRateManage energyRateManage) {
        return R.ok(energyRateManageService.queryPage(page, energyRateManage));
    }


    /**
     * 通过id查询能源费率管理
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(energyRateManageService.getById(id));
    }

    /**
     * 新增能源费率管理
     *
     * @param energyRateManage 能源费率管理
     * @return R
     */
    @Operation(summary = "新增能源费率管理", description = "新增能源费率管理")
    @SysLog("新增能源费率管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_add')")
    public R save(@RequestBody EnergyRateManage energyRateManage) {
        return R.ok(energyRateManageService.saveAndUpdate(energyRateManage));
    }

    /**
     * 修改能源费率管理
     *
     * @param energyRateManage 能源费率管理
     * @return R
     */
    @Operation(summary = "修改能源费率管理", description = "修改能源费率管理")
    @SysLog("修改能源费率管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_edit')")
    public R updateById(@RequestBody EnergyRateManage energyRateManage) {
        return R.ok(energyRateManageService.updateById(energyRateManage));
    }

    /**
     * 通过id删除能源费率管理
     *
     * @param rateId rateId
     * @return R
     */
    @Operation(summary = "通过id删除能源费率管理", description = "通过id删除能源费率管理")
    @SysLog("通过id删除能源费率管理")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_del')")
    public R removeById(String rateId) {
        System.out.println(rateId);
        boolean result = energyRateManageService.remove(Wrappers.<EnergyRateManage>lambdaQuery().eq(EnergyRateManage::getRateId, rateId));
        if (result == true) {
            result = energyRateFlowService.remove(Wrappers.<EnergyRateFlow>lambdaQuery().eq(EnergyRateFlow::getRateId, rateId));
        }
        return R.ok(result, "删除成功");
    }


    /**
     * 导出excel 表格
     *
     * @param energyRateManage 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('exsecs_energyRateManage_export')")
    public List<EnergyRateManageVo> export(EnergyRateManage energyRateManage) {
        return energyRateManageService.export(energyRateManage);
    }
}